package com.esisa.swing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.lang.reflect.Field;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Form extends JPanel {
	private int labelWidth = 100;
	private JPanel content;
	public Form() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		content = new JPanel();
		this.add(content);
		content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
		//setBorder(BorderFactory.createLineBorder(Color.blue));
		//content.setBorder(BorderFactory.createLineBorder(Color.green));
	}
	
	public Form(int labelWidth) {
		this();
		this.labelWidth = labelWidth;
	}
	
	public Form(Class c) {
		this();
		Field t[] = c.getDeclaredFields();
		for (int i = 0; i < t.length; i++) {
			//System.out.println(t[i].getName());
			addField(t[i].getName(), 20);
		}
	}
	
	public Form(Class c, int labelWidth) {
		this();
		this.labelWidth = labelWidth;
		Field t[] = c.getDeclaredFields();
		for (int i = 0; i < t.length; i++) {
			//System.out.println(t[i].getName());
			addField(t[i].getName(), 20);
		}
	}
	public Form(Class c, int labelWidth, int size) {
		this();
		this.labelWidth = labelWidth;
		Field t[] = c.getDeclaredFields();
		for (int i = 0; i < t.length; i++) {
			//System.out.println(t[i].getName());
			addField(t[i].getName(), size);
		}
	}
	
	public Form(String s[]){
		this();
		for (int i = 0; i < s.length; i++) {
			addField(s[i], 20);
		}
	}
	public Form(String s[], int width){
		this();
		this.labelWidth = width;
		for (int i = 0; i < s.length; i++) {
			addField(s[i], 20);
		}
		
	}
	public Form(String s[], int size[]){
		this();
		for (int i = 0; i < s.length; i++) {
			addField(s[i], size[i]);
		}
		
	}
	public Form(String s[], int size[], int width){
		this();
		this.labelWidth = width;
		for (int i = 0; i < s.length; i++) {
			addField(s[i], size[i]);
		}	
	}
	
	public Form(String s[], int LabelWidth, int size){
		this();
		this.labelWidth = LabelWidth;
		for (int i = 0; i < s.length; i++) {
			addField(s[i],size);
		}
	}
	
	public Form(Item item) {
		this();
		for (int i = 0; i < item.getFieldsCount(); i++) {
			addField(item.getField(i), item.getFieldSize(i));
		}
	}
	
	public Form(Item item, int labelWidth) {
		this();
		this.labelWidth = labelWidth;
		for (int i = 0; i < item.getFieldsCount(); i++) {
			addField(item.getField(i), item.getFieldSize(i));
		}
	}
	public int getLabelWidth() {
		return labelWidth;
	}

	public void setLabelWidth(int labelWidth) {
		this.labelWidth = labelWidth;
	}

	public void addField(String label, int size) {
		LabeledTextField t1 = new LabeledTextField(label, size, labelWidth);
		content.add(t1);
	}
	
	public LabeledTextField getLabeledTextField(String label){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				return L;				
			}
		}
		return null;
	}
	
	public JTextField getJTextField(String label){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				return L.getTextField();				
			}
		}
		return null;
	}
	
	public String getText(String label){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				return L.getText();				
			}
		}
		return null;
	}
	
	public void setText(String label, String text){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				L.setText(text);
				return;
			}
		}
	}
	
	public void AddKeyListner(String label, KeyListener listner){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				L.AddKeyListner(listner);
				return;
			}
		}
	}
	
	public void AddKeyListner(KeyListener listner){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			L.AddKeyListner(listner);
		}
	}
	
	public void AddMouseListner(String label, MouseListener listner){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			if(L.getLabelText().equalsIgnoreCase(label)){
				L.AddMouseListner(listner);
				return;
			}
		}
	}
	
	public void AddMouseListner(MouseListener listner){
		for (int i = 0; i < content.getComponentCount(); i++) {
			LabeledTextField L = (LabeledTextField)content.getComponent(i);
			L.AddMouseListner(listner);
		}
	}
	
	public int length(){
		return content.getComponentCount();
	}
	
	public String getText(int index){
		LabeledTextField l=(LabeledTextField) content.getComponent(index);
		return l.getText();
	}
	
	public void setText(int index, String s){
		LabeledTextField l=(LabeledTextField) content.getComponent(index);
		l.setText(s);
	}
	
	public String getLabelText(int index){
		LabeledTextField l=(LabeledTextField) content.getComponent(index);
		return l.getLabelText();
	}
	
}
